import db from '../models/index.js';
import bcrypt from 'bcryptjs';

const { User, Course, Enrollment } = db;

async function initData() {
  try {
    console.log('开始初始化数据...');

    // 检查是否已有管理员用户
    const existingAdmin = await User.findOne({ where: { username: 'admin' } });
    if (existingAdmin) {
      console.log('数据已存在，跳过初始化');
      return;
    }

    // 创建管理员用户
    const adminPassword = await bcrypt.hash('admin123', 12);
    const admin = await User.create({
      username: 'admin',
      password: adminPassword,
      name: '系统管理员',
      email: 'admin@school.edu',
      role: 'admin',
      isActive: true
    });
    console.log('管理员用户创建成功');

    // 创建教师用户
    const teacherPassword = await bcrypt.hash('teacher123', 12);
    const teachers = await User.bulkCreate([
      {
        username: 'teacher1',
        password: teacherPassword,
        name: '张教授',
        email: 'zhang@school.edu',
        role: 'teacher',
        isActive: true
      },
      {
        username: 'teacher2',
        password: teacherPassword,
        name: '李教授',
        email: 'li@school.edu',
        role: 'teacher',
        isActive: true
      },
      {
        username: 'teacher3',
        password: teacherPassword,
        name: '王教授',
        email: 'wang@school.edu',
        role: 'teacher',
        isActive: true
      }
    ]);
    console.log('教师用户创建成功');

    // 创建学生用户
    const studentPassword = await bcrypt.hash('student123', 12);
    const students = await User.bulkCreate([
      {
        username: 'student1',
        password: studentPassword,
        name: '张三',
        email: 'zhangsan@school.edu',
        role: 'student',
        isActive: true
      },
      {
        username: 'student2',
        password: studentPassword,
        name: '李四',
        email: 'lisi@school.edu',
        role: 'student',
        isActive: true
      },
      {
        username: 'student3',
        password: studentPassword,
        name: '王五',
        email: 'wangwu@school.edu',
        role: 'student',
        isActive: true
      }
    ]);
    console.log('学生用户创建成功');

    // 创建课程
    const courses = await Course.bulkCreate([
      {
        code: 'CS101',
        name: '计算机科学导论',
        description: '计算机科学基础入门课程，涵盖编程、算法、数据结构等基础知识',
        credit: 3,
        capacity: 50,
        currentEnrollment: 0,
        location: '逸夫楼101',
        schedule: '周一 1-2节',
        status: 'published',
        teacherId: teachers[0].id,
        startDate: '2024-09-01',
        endDate: '2025-01-15'
      },
      {
        code: 'MATH201',
        name: '高等数学',
        description: '理工科基础数学课程，包括微积分、线性代数等内容',
        credit: 4,
        capacity: 100,
        currentEnrollment: 0,
        location: '数学楼201',
        schedule: '周二 3-4节',
        status: 'published',
        teacherId: teachers[1].id,
        startDate: '2024-09-01',
        endDate: '2025-01-15'
      },
      {
        code: 'ENG101',
        name: '大学英语',
        description: '大学英语基础课程，提高学生英语听说读写能力',
        credit: 2,
        capacity: 80,
        currentEnrollment: 0,
        location: '外语楼101',
        schedule: '周三 5-6节',
        status: 'published',
        teacherId: teachers[2].id,
        startDate: '2024-09-01',
        endDate: '2025-01-15'
      },
      {
        code: 'PHYS101',
        name: '大学物理',
        description: '理工科基础物理课程，包括力学、电磁学等内容',
        credit: 3,
        capacity: 60,
        currentEnrollment: 0,
        location: '物理楼101',
        schedule: '周四 1-2节',
        status: 'published',
        teacherId: teachers[0].id,
        startDate: '2024-09-01',
        endDate: '2025-01-15'
      }
    ]);
    console.log('课程创建成功');

    // 创建一些选课记录
    await Enrollment.bulkCreate([
      {
        studentId: students[0].id,
        courseId: courses[0].id,
        status: 'selected',
        enrolledAt: new Date()
      },
      {
        studentId: students[0].id,
        courseId: courses[1].id,
        status: 'selected',
        enrolledAt: new Date()
      },
      {
        studentId: students[1].id,
        courseId: courses[0].id,
        status: 'selected',
        enrolledAt: new Date()
      },
      {
        studentId: students[1].id,
        courseId: courses[2].id,
        status: 'selected',
        enrolledAt: new Date()
      }
    ]);

    // 更新课程的当前选课人数
    await Course.update({ currentEnrollment: 2 }, { where: { id: courses[0].id } });
    await Course.update({ currentEnrollment: 1 }, { where: { id: courses[1].id } });
    await Course.update({ currentEnrollment: 1 }, { where: { id: courses[2].id } });

    console.log('选课记录创建成功');
    console.log('数据初始化完成！');
    
    console.log('\n用户账号信息：');
    console.log('管理员: admin / admin123');
    console.log('教师: teacher1 / teacher123');
    console.log('学生: student1 / student123');

  } catch (error) {
    console.error('数据初始化失败:', error);
  }
}

export default initData; 